package show.me.the.money.resource;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class BinaryFile extends ResourceFile {
	byte[] buf;
	File file;
	
	public void setFileName(String fileName){
		file = new File(fileName);
		exists = file.exists();
	}
	
	public int length(){
		if(buf == null){
			return 0;
		}
		return buf.length;
	}
	
	public boolean exists(){
		return exists;
	}
	public InputStream getInputStream(){
		return new ByteArrayInputStream(buf);
	}
	
	public void load() throws IOException{
		if(exists && file.lastModified() != lastModi){
			lastModi = file.lastModified();
			InputStream  in=new FileInputStream(file);
			int len=(int)file.length();
			buf = new byte[len];
			in.read(buf);
			in.close();
		}
	}
	
	public void writeToStream(OutputStream outs,boolean needClose) throws IOException{
	    if(buf != null){
			outs.write(buf,0,buf.length);
			if(needClose){
				outs.flush();
				outs.close();
			}
	    }
	}

}
